<template>
  <div class="add">
        <h1 align="center">修改个人信息</h1>
        <span>旧用户名:</span>
        <el-input v-model="oldUser.userName" placeholder="请输入旧用户名"></el-input>
        <div style="margin: 30px 0;"></div>
        <span>旧密码:</span>
        <el-input placeholder="请输入旧密码" v-model="oldUser.userPwd" show-password></el-input>
        <div style="margin: 30px 0;"></div>
        <span>新用户名:</span>
        <el-input v-model="newUser.userName" placeholder="请输入新用户名"></el-input>
        <div style="margin: 30px 0;"></div>
        <span>新密码:</span>
        <el-input v-model="newUser.userPwd" placeholder="请输入新密码" show-password></el-input>
        <div style="margin: 30px 0;"></div>
        <center><el-button align="center" type="primary" plain @click="modify()">修改信息</el-button></center>
  </div>
</template>

<script>
export default {
    name:"Modify",
    data() {
        return {
            user:{
                userId:'',
                userName:'',
                userPwd:'',
            },
            oldUser: {
                userName: '',
                userPwd:'',
            },
            newUser: {
                userId:'',
                userName: '',
                userPwd:'',
            },
        };
    },
    methods:{
       modify(){
            if(this.user.userName==this.oldUser.userName&&this.user.userPwd==this.oldUser.userPwd){
                if(this.newUser.userName==this.oldUser.userName){
                    this.newUser.userId=this.user.userId;
                    this.user=this.newUser;
                    console.log(this.user);
                    this.$axios.post("http://120.77.71.211:9090/mynewsweb/api/updateUser1", this.user).then((res) => {
                        if (res.data.state) {
                            localStorage.removeItem("user");
                            localStorage.setItem("user",JSON.stringify(res.data.user));
                            var userString = localStorage.getItem("user");
                            if (userString) {
                                this.user = JSON.parse(userString);
                            }
                            alert(res.data.msg);
                        } else {
                            alert(res.data.msg);
                        }
                    });
                }else{
                    this.newUser.userId=this.user.userId;
                    this.user=this.newUser;
                    console.log(this.user);
                    this.$axios.post("http://120.77.71.211:9090/mynewsweb/api/updateUser2", this.user).then((res) => {
                        if (res.data.state) {
                            localStorage.removeItem("user");
                            localStorage.setItem("user",JSON.stringify(res.data.user));
                            var userString = localStorage.getItem("user");
                            if (userString) {
                                this.user = JSON.parse(userString);
                            }
                            alert(res.data.msg);
                        } else {
                            alert(res.data.msg);
                        }
                    });
                }
            }else{
                alert("输入用户名或密码错误")
            }
        },
        returnMain(){
            this.$router.push("/mainTable");
        }
    },
    created(){
      var userString = localStorage.getItem("user");
      if (userString) {
                this.user = JSON.parse(userString);
      }
  }
}
</script>

<style>
    .add{
      width: 50%;
      margin:0 auto;
    }
</style>